﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TravelLink.DAL;

namespace TravelLink.WebApp.Admin {
    public partial class AdminUserManager : System.Web.UI.Page, ICallbackEventHandler
    {
        protected void Page_Load(object sender, EventArgs e) {
           this.CheckResponse("1001");
            if (!this.IsPostBack)
            {
                adefrom.Value = DateTime.Now.AddYears(-1);
                adeto.Value = DateTime.Now;
                this.achkCreatedResume.Value = 2;
                if ((this.Request.QueryString["UserCode"] ?? "") != string.Empty)
                {
                    this.LinqDataSource1.Where = "UserCode=@UserCode";
                    this.LinqDataSource1.WhereParameters.Add("UserCode", System.Data.DbType.Guid, this.Request.QueryString["UserCode"]);
                }   
            }
        }

        protected void ASPxGridView1_HtmlDataCellPrepared(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableDataCellEventArgs e)
        {
            if (e.VisibleIndex < 0)
            {
                return;
            }
            TL_User obj = this.ASPxGridView1.GetRow(e.VisibleIndex) as TL_User;
            if (e.DataColumn.Caption == "简历创建")
            {
                e.Cell.Text = obj.TL_Resume.Count > 0 ? "有" : "无";
            }
            if (e.DataColumn.Caption == "激活")
            {
                e.Cell.Text = (obj.Status??0) > 0 ? "是" : "否";
            }
            if (e.DataColumn.Caption == "姓名")
            {
                e.Cell.Text = (obj.CLastName ?? "") + (obj.CFirstName ?? "");
                if (e.Cell.Text == null || e.Cell.Text == string.Empty)
                {
                    e.Cell.Text = " ";
                }
            }
        }

        protected void ASPxGridView1_RowDeleted(object sender, DevExpress.Web.Data.ASPxDataDeletedEventArgs e) {
            e.ExceptionHandled = true;
            
        }

        protected void ASPxGridView1_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e) {
            Guid userCode = (Guid)e.Keys[0];

            var db = DataContextFactory.Create();
            var entity = db.TL_User.Where(u => u.UserCode == userCode).FirstOrDefault();
            if (entity != null) {
                db.TL_User.DeleteOnSubmit(entity);
                db.SubmitChanges();
            }
        }

        protected void abtnSearch_Click(object sender, EventArgs e)
        {
            this.LinqDataSource1.Where = "1==1";
            if (this.adefrom.Date != DateTime.MinValue)
            {
                this.LinqDataSource1.Where += @"&&CreateDate>=@datefrom";
                this.LinqDataSource1.WhereParameters.Add("datefrom", System.Data.DbType.DateTime, this.adefrom.Date.ToString("yyyy-MM-dd 00:00:00"));
            }
            if (this.adeto.Date != DateTime.MinValue)
            {
                this.LinqDataSource1.Where += @"&&CreateDate<=@dateto";
                this.LinqDataSource1.WhereParameters.Add("dateto", System.Data.DbType.DateTime, this.adeto.Date.ToString("yyyy-MM-dd 23:59:59"));
            }
            if (this.achkCreatedResume.Value != null)
            {
                if (this.achkCreatedResume.Value.ToString() == "1")
                {
                    this.LinqDataSource1.Where += "&&TL_Resume.Count>0";
                }
                else if (this.achkCreatedResume.Value.ToString() == "0")
                {
                    this.LinqDataSource1.Where += "&&TL_Resume.Count=0";
                }
            }
            if (this.atbmail.Text.Trim() != string.Empty)
            {
                this.LinqDataSource1.Where += "&&Email==@Mail";
                this.LinqDataSource1.WhereParameters.Add("Mail", this.atbmail.Text);
            }
            if (this.atbmobile.Text.Trim() != string.Empty)
            {
                this.LinqDataSource1.Where += "&&Mobile==@Mobile";
                this.LinqDataSource1.WhereParameters.Add("Mobile", this.atbmobile.Text);
            }
        }

        protected void ASPxGridView1_HtmlCommandCellPrepared(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewTableCommandCellEventArgs e)
        {
          if( ((DevExpress.Web.ASPxEditors.WebColumnBase)(e.CommandColumn)).Caption=="简历")
          {
            
          }
        }

        protected void ASPxButton1_Click(object sender, EventArgs e)
        {
            this.ASPxGridViewExporter1.WriteXlsToResponse(true);
        }

        private string CallBackValue = string.Empty;

        #region ICallbackEventHandler 成员

        public string GetCallbackResult()
        {
            return CallBackValue;
        }

        public void RaiseCallbackEvent(string eventArgument)
        {
            try
            {
                string[] eArg = eventArgument.Split('|');
                int rowid = int.Parse(eArg[0]);
                var dc = DAL.DataContextFactory.Create();
                TL_User obj = (TL_User)this.ASPxGridView1.GetRow(rowid);
                var user=dc.TL_User.Where(t => t.UserCode == obj.UserCode).FirstOrDefault();
                user.Status = 1;
                dc.SubmitChanges();                
                CallBackValue = "";
            }
            catch (Exception ex)
            {
                this.CallBackValue = ex.Message;
            }
        }

        #endregion
    }
}